/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package formatters;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import presentation.CurrencyElement;
import presentation.DateElement;
import presentation.LinkElement;
import presentation.PresentationElement;
import presentation.TextElement;

/**
 *
 * @author Alejandro
 */
public class EditFormatter implements Formatter {
    private String output;

    @Override
    public String format(PresentationElement e) {
        e.acceptEdit(this);
        return output;
    }

    public void formatText(PresentationElement e) {
        TextElement text = (TextElement) e;
        output = "<label for=\"edit" + text.getName() + "\">" + text.getName() + "</label><input type=\"text\" id=\"edit" + text.getName() + "\" name=\"edit" + text.getName() + "\" value=\"" + text.getValue() + "\" />";
    }

    public void formatLink(PresentationElement e) {
        LinkElement link = (LinkElement) e;
        output = "<input type=\"text\" id=\"edit" + link.getName() + "\" name=\"edit" + link.getName() + "\" value=\"" + link.getValue() + "\" />";
    }

    public void formatCurrency(PresentationElement e) {
        CurrencyElement currency = (CurrencyElement) e;
        output = "<label for=\"edit" + currency.getName() + "\">" + currency.getName() + " $</label><input type=\"text\" id=\"edit" + currency.getName() + "\" name=\"edit" + currency.getName() + "\" value=\"" + currency.getValue() + "\" />";
    }

    public void formatDate(PresentationElement e) {
        try {
            DateElement date = (DateElement) e;
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
            Calendar c = Calendar.getInstance();
            c.setTime(sdf.parse(date.getValue()));

            StringBuilder daySelect = new StringBuilder();
            daySelect.append("<select id=\"" + date.getName() + "day\" name=\"" + date.getName() + "day\" size=\"1\">");
            for (int i=1; i<32; i++) {
                daySelect.append("<option value=\"");
                daySelect.append(i);
                if (c.get(Calendar.DAY_OF_MONTH) == i)
                    daySelect.append("\" selected=\"selected\">");
                else
                    daySelect.append("\">");
                daySelect.append(i);
                daySelect.append("</option>");
            }
            daySelect.append("</select>");

            String months [] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
            StringBuilder monthSelect = new StringBuilder();
            monthSelect.append("<select id=\"" + date.getName() + "month\" name=\"" + date.getName() + "month\" size=\"1\">");
            for (int i=1; i<13; i++) {
                monthSelect.append("<option value=\"");
                monthSelect.append(i);
                if (c.get(Calendar.MONTH)+1 == i)
                    monthSelect.append("\" selected=\"selected\">");
                else
                    monthSelect.append("\">");
                monthSelect.append(months[i-1]);
                monthSelect.append("</option>");
            }
            monthSelect.append("</select>");

            StringBuilder yearSelect = new StringBuilder();
            yearSelect.append("<select id=\"" + date.getName() + "year\" name=\"" + date.getName() + "year\" size=\"1\">");
            for (int i=1970; i<2012; i++) {
                yearSelect.append("<option value=\"");
                yearSelect.append(i);
                if (c.get(Calendar.YEAR) == i)
                    yearSelect.append("\" selected=\"selected\">");
                else
                    yearSelect.append("\">");
                yearSelect.append(i);
                yearSelect.append("</option>");
            }
            yearSelect.append("</select>");
            
            output = daySelect.toString() + monthSelect.toString() + yearSelect.toString();
        } catch (ParseException ex) {
        }
    }
}
